//
//  NewsDetailTapImgView.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/10/3.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "NewsDetailTapImgView.h"
#import "NewsDetailViewController.h"
#import "PhotoShowViewController.h"

@implementation NewsDetailTapImgView {
    UIImageView* imgView;           //新创建的imgView，全屏显示
    
    UIView* grayView;           //灰色笼罩
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self _addTapGes];
    }
    
    return self;
}

- (instancetype)initWithImage:(UIImage *)image {
    if (self = [super initWithImage:image]) {
        [self _addTapGes];
    }
    
    return self;
}

- (void)_addTapGes {
    self.userInteractionEnabled = YES;
    //为这个imgVeiw添加单击手势
    UITapGestureRecognizer* tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgViewTapGestureAction)];
    [self addGestureRecognizer:tapGes];
}

//为自己添加单击手势,如果是图集，点击就进入图集，否则就放大到全屏幕观看
- (void)imgViewTapGestureAction {
    //判断是图集，还是只是一张普通的照片

    if (_isPhotoSet) {
        //根据响应者链找到NewsDetailVC这个类，push到要观看的图集
        NewsDetailViewController* newsDetailVC = [Tools className:@"NewsDetailViewController" fromResponder:self.nextResponder];
        if (newsDetailVC) {
            PhotoShowViewController* photoShowVC = [[PhotoShowViewController alloc] init];
            photoShowVC.photoSetId = _skipId;
            [newsDetailVC.navigationController pushViewController:photoShowVC animated:YES];
        }
        
    } else {
        [self zoomIn];
    }
}

- (void)zoomIn {
    //创建灰色笼罩背景
    grayView = [[UIView alloc] initWithFrame:self.window.bounds];
    grayView.userInteractionEnabled = YES;
    grayView.backgroundColor = [UIColor blackColor];
    grayView.alpha = 0.01;
    [self.window addSubview:grayView];
    
    
    //获得self在其父视图上的frame,并转化到window上
    CGRect frame = [self convertRect:self.bounds toView:self.window];
    
    // 创建imgView并贴在window上
    imgView = [[UIImageView alloc] initWithFrame:frame];
    imgView.image = self.image;
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    imgView.userInteractionEnabled = YES;
    [self.window addSubview:imgView];
    //    imgView.backgroundColor = [UIColor greenColor];
    
    [UIView animateWithDuration:0.5 animations:^{
        grayView.alpha = 0.7;
        
        //计算出图片放大后应该有的高度
        CGFloat height = self.image.size.height/self.image.size.width*kWidth;
        [imgView setFrame:CGRectMake(0, (kHeight-height)/2, kWidth, height)];
    }];
    
    
    //为灰色笼罩视图添加手势，点击后缩回然后移除
    UITapGestureRecognizer* tapZoomOut1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zoomOut)];
    [grayView addGestureRecognizer:tapZoomOut1];
    //imgView也添加这个单击手势
    UITapGestureRecognizer* tapZoomOut2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zoomOut)];
    [imgView addGestureRecognizer:tapZoomOut2];
}

- (void)zoomOut {
    //获得self在其父视图上的frame,并转化到window上
    CGRect frame = [self convertRect:self.bounds toView:self.window];
    
    [UIView animateWithDuration:0.5 animations:^{
        grayView.alpha = 0.01;
        [imgView setFrame:frame];
    } completion:^(BOOL finished) {
        if (finished) {
            [imgView removeFromSuperview];
            [grayView removeFromSuperview];
            grayView = nil;
            imgView = nil;
        }
    }];
}


@end
